---
keywords: ""
layout: "default"
title: "RawRepresentable"
description: "Swift documentation for 'RawRepresentable'"
root: "/v5.1"
---

<div class="intro-declaration"><code class="language-swift">protocol RawRepresentable</code></div><p>A type that can be converted to and from an associated raw value.</p>
<table class="standard"><tr><th id="conforming-types">Conforming Types</th><td><code class="inherits">CodingUserInfoKey, OptionSet</code></td></tr><tr><th id="associatedtypes">Associated Types</th><td><span id="aliasesmark"></span><div class="declaration"><code class="language-swift">associatedtype RawValue</code><div class="comment"><p>Every distinct value of the conforming type has a corresponding unique
value of the <code>RawValue</code> type, but there may be values of the <code>RawValue</code>
type that don't have a corresponding value of the conforming type.</p>
</div></div></td></tr></table><div class="discussion comment"><p>With a <code>RawRepresentable</code> type, you can switch back and forth between a
custom type and an associated <code>RawValue</code> type without losing the value of
the original <code>RawRepresentable</code> type. Using the raw value of a conforming
type streamlines interoperation with Objective-C and legacy APIs and
simplifies conformance to other protocols, such as <code>Equatable</code>,
<code>Comparable</code>, and <code>Hashable</code>.</p>
<p>The <code>RawRepresentable</code> protocol is seen mainly in two categories of types:
enumerations with raw value types and option sets.</p>
<h3>Enumerations with Raw Values</h3>
<p>For any enumeration with a string, integer, or floating-point raw type, the
Swift compiler automatically adds <code>RawRepresentable</code> conformance. When
defining your own custom enumeration, you give it a raw type by specifying
the raw type as the first item in the enumeration's type inheritance list.
You can also use literals to specify values for one or more cases.</p>
<p>For example, the <code>Counter</code> enumeration defined here has an <code>Int</code> raw value
type and gives the first case a raw value of <code>1</code>:</p>
<pre><code class="language-swift">enum Counter: Int {
    case one = 1, two, three, four, five
}
</code></pre>
<p>You can create a <code>Counter</code> instance from an integer value between 1 and 5
by using the <code>init?(rawValue:)</code> initializer declared in the
<code>RawRepresentable</code> protocol. This initializer is failable because although
every case of the <code>Counter</code> type has a corresponding <code>Int</code> value, there
are many <code>Int</code> values that <em>don't</em> correspond to a case of <code>Counter</code>.</p>
<pre><code class="language-swift">for i in 3...6 {
    print(Counter(rawValue: i))
}
// Prints &quot;Optional(Counter.three)&quot;
// Prints &quot;Optional(Counter.four)&quot;
// Prints &quot;Optional(Counter.five)&quot;
// Prints &quot;nil&quot;
</code></pre>
<h3>Option Sets</h3>
<p>Option sets all conform to <code>RawRepresentable</code> by inheritance using the
<code>OptionSet</code> protocol. Whether using an option set or creating your own,
you use the raw value of an option set instance to store the instance's
bitfield. The raw value must therefore be of a type that conforms to the
<code>FixedWidthInteger</code> protocol, such as <code>UInt8</code> or <code>Int</code>. For example, the
<code>Direction</code> type defines an option set for the four directions you can
move in a game.</p>
<pre><code class="language-swift">struct Directions: OptionSet {
    let rawValue: UInt8

    static let up    = Directions(rawValue: 1 &lt;&lt; 0)
    static let down  = Directions(rawValue: 1 &lt;&lt; 1)
    static let left  = Directions(rawValue: 1 &lt;&lt; 2)
    static let right = Directions(rawValue: 1 &lt;&lt; 3)
}
</code></pre>
<p>Unlike enumerations, option sets provide a nonfailable <code>init(rawValue:)</code>
initializer to convert from a raw value, because option sets don't have an
enumerated list of all possible cases. Option set values have
a one-to-one correspondence with their associated raw values.</p>
<p>In the case of the <code>Directions</code> option set, an instance can contain zero,
one, or more of the four defined directions. This example declares a
constant with three currently allowed moves. The raw value of the
<code>allowedMoves</code> instance is the result of the bitwise OR of its three
members' raw values:</p>
<pre><code class="language-swift">let allowedMoves: Directions = [.up, .down, .left]
print(allowedMoves.rawValue)
// Prints &quot;7&quot;
</code></pre>
<p>Option sets use bitwise operations on their associated raw values to
implement their mathematical set operations. For example, the <code>contains()</code>
method on <code>allowedMoves</code> performs a bitwise AND operation to check whether
the option set contains an element.</p>
<pre><code class="language-swift">print(allowedMoves.contains(.right))
// Prints &quot;false&quot;
print(allowedMoves.rawValue &amp; Directions.right.rawValue)
// Prints &quot;0&quot;
</code></pre>
</div><h3>Initializers</h3><div id="init_rawValue-d16a3c3e47fae7437ff5bc15a3063a32" class="declaration"><a class="toggle-link" href="#comment-init_rawValue-d16a3c3e47fae7437ff5bc15a3063a32">init init?(rawValue:)</a> <span class="required">Required</span><div class="comment collapse in" id="comment-init_rawValue-d16a3c3e47fae7437ff5bc15a3063a32"><p>Creates a new instance with the specified raw value.</p>
<p>If there is no value of the type that corresponds with the specified raw
value, this initializer returns <code>nil</code>. For example:</p>
<pre><code class="language-swift">enum PaperSize: String {
    case A4, A5, Letter, Legal
}

print(PaperSize(rawValue: &quot;Legal&quot;))
// Prints &quot;Optional(&quot;PaperSize.Legal&quot;)&quot;

print(PaperSize(rawValue: &quot;Tabloid&quot;))
// Prints &quot;nil&quot;
</code></pre>
<ul>
<li>Parameter rawValue: The raw value to use for the new instance.</li>
</ul>
<h4>Declaration</h4><code class="language-swift">init?(rawValue: Self.RawValue)</code></div></div><h3>Instance Variables</h3><div id="rawValue-05d5e54af177d42e80ca02e20dada809" class="declaration"><a class="toggle-link" href="#comment-rawValue-05d5e54af177d42e80ca02e20dada809">var rawValue</a> <span class="required">Required</span><div class="comment collapse in" id="comment-rawValue-05d5e54af177d42e80ca02e20dada809"><p>The corresponding value of the raw type.</p>
<p>A new instance initialized with <code>rawValue</code> will be equivalent to this
instance. For example:</p>
<pre><code class="language-swift">enum PaperSize: String {
    case A4, A5, Letter, Legal
}

let selectedSize = PaperSize.Letter
print(selectedSize.rawValue)
// Prints &quot;Letter&quot;

print(selectedSize == PaperSize(rawValue: selectedSize.rawValue)!)
// Prints &quot;true&quot;
</code></pre>
<h4>Declaration</h4><code class="language-swift">var rawValue: Self.RawValue</code></div></div>